#!/bin/bash

LONGNAME="Connection logging"
NAME="conn_sl"
DAEMON="$TWMFOLDER/modules/nf_conntrack/$NAME.pl"

PID=''
function setPID
{
	PID=`ps ax | grep $NAME.pl | grep -v grep | grep -v "cgi-bin" | gawk '{print $1}'`
}

set -e

case $1 in
start)
	setPID
	if [ ! -n "$PID" ]; then
		echo -n "Starting $LONGNAME: ";
		perl $DAEMON #--background
		echo "............................done";
	else
		echo "WARNING! Daemon is started already with pid $PID"
	fi
	;;
stop)
	setPID
	if [ ! -n "$PID" ]; then
		echo "Deamon $LONGNAME was not started"
	else
		echo "Stopping $LONGNAME (pid is $PID):"
		kill -s INT $PID
		echo "............................done";
	fi
	;;
restart)
        shift
        $0 stop
	sleep 3
        $0 start "$@"
        ;;
init)
	setPID
	if [ ! -n "$PID" ]; then
		echo "WARNING! $LONGNAME daemon is not started."
	        $0 start "$@"
	fi
	setPID
	if [ -n "$PID" ]; then
		kill -s USR1 $PID
	fi
        ;;
clear)
	setPID
	if [ ! -n "$PID" ]; then
		echo "WARNING! $LONGNAME daemon is not started."
	        $0 start "$@"
	fi
	setPID
	if [ -n "$PID" ]; then
		kill -s USR2 $PID
	fi
        ;;
log)
	setPID
	if [ ! -n "$PID" ]; then
		echo "WARNING! $LONGNAME daemon is not started."
	        $0 start "$@"
	fi
	setPID
	if [ -n "$PID" ]; then
		kill -s PWR $PID
	fi
        ;;
*)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|init|clear|log}" >&2
	exit 1
	;;
esac

exit 0

# EOF